unit U_ConsPlano;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBTables, ExtCtrls, StdCtrls, Buttons, Grids, DBGrids, ComCtrls;

type
  Tf_consplano = class(TForm)
    bs: TStatusBar;
    Panel1: TPanel;
    dbg: TDBGrid;
    Panel3: TPanel;
    Label1: TLabel;
    edtDescricao: TEdit;
    Panel2: TPanel;
    btnFechar: TBitBtn;
    btnBuscar: TBitBtn;
    btnCancelar: TBitBtn;
    Panel4: TPanel;
    rdgOrdem: TRadioGroup;
    Timer1: TTimer;
    ds: TDataSource;
    q: TQuery;
    dbctb: TDatabase;
    qEMP_CODIGO: TStringField;
    qPLANO_CODIGO: TStringField;
    qPLANO_REDUZIDO: TStringField;
    qPLANO_DESCRICAO: TStringField;
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure rdgOrdemClick(Sender: TObject);
    procedure dbgKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnBuscarClick(Sender: TObject);
    procedure edtDescricaoExit(Sender: TObject);
    procedure edtDescricaoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnCancelarClick(Sender: TObject);
    procedure btnFecharClick(Sender: TObject);
  private
    { Private declarations }
  public
    emp_codigo, xreduzido: string;
    ordem: Integer;
    enter, ok: boolean;
    { Public declarations }
  end;

var
  f_consplano: Tf_consplano;

implementation

{$R *.DFM}

procedure Tf_consplano.FormActivate(Sender: TObject);
begin
  Bs.Panels.Items[2].Text := datetostr(date);
  Bs.Panels.Items[3].Text := timetostr(time);
  dbctb.Connected := True;
  rdgOrdem.ItemIndex := ordem;
  enter := False;
  q.sql.clear;
  q.params.clear;
  q.active := False;
  q.sql.add('select * from plano where emp_codigo=:k_emp_codigo');
  if rdgOrdem.ItemIndex = 0 then begin
    q.sql.add('order by plano_descricao');
  end else begin
    q.sql.add('order by plano_codigo');
  end;
  q.ParamByName('k_emp_codigo').asstring := emp_codigo;
  q.open;
end;

procedure Tf_consplano.FormCreate(Sender: TObject);
begin
  ordem := 0;
end;

procedure Tf_consplano.rdgOrdemClick(Sender: TObject);
begin
  q.close;
  q.sql.clear;
  q.params.clear;
  q.DatabaseName := dbctb.DatabaseName;
  q.sql.add('select * from plano where emp_codigo=:k_emp_codigo');
  if rdgOrdem.ItemIndex = 0 then begin
    q.sql.add('order by plano_descricao');
  end else begin
    q.sql.add('order by plano_codigo');
  end;
  q.ParamByName('k_emp_codigo').asstring := emp_codigo;
  q.open;
  dbg.SetFocus;
end;

procedure Tf_consplano.dbgKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  cont: Integer;
begin
  if key = VK_F2 then begin
    btnBuscar.OnClick(Sender);
  end else if key = VK_ESCAPE then begin
    btnCancelar.OnClick(Sender);
  end else if key = 12 then begin
    btnfechar.onclick(sender);
  end else if key = VK_F9 then begin
    cont := rdgOrdem.ItemIndex;
    cont := cont + 1;
    if cont > (rdgOrdem.Items.Count - 1) then cont := 0;
    rdgOrdem.ItemIndex := cont;
    rdgOrdem.Onclick(Sender);
  end;
end;

procedure Tf_consplano.btnBuscarClick(Sender: TObject);
begin
  panel3.Visible := True;
  edtDescricao.SetFocus;
end;

procedure Tf_consplano.edtDescricaoExit(Sender: TObject);
begin
  panel3.visible := False;
end;

procedure Tf_consplano.edtDescricaoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = 12 then begin
    q.Locate('plano_descricao', edtdescricao.text, [loPartialKey]);
    btnFechar.SetFocus;
  end;
  if key = VK_ESCAPE then begin
    dbg.SetFocus;
  end;
end;

procedure Tf_consplano.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  dbctb.Connected := False;
end;

procedure Tf_consplano.btnCancelarClick(Sender: TObject);
begin
  ok := False;
  q.Close;
  q.Active := false;
  dbctb.Connected := false;
  close;
end;

procedure Tf_consplano.btnFecharClick(Sender: TObject);
begin
  xreduzido := dbg.Fields[1].asstring;
  ok := True;
  q.Close;
  q.Active := false;
  dbctb.Connected := false;
  close;
end;

end.
